package com.cxs.qrwidgedemo.slice;

import com.cxs.qrwidgedemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
// 二维码相关的类
import ohos.agp.components.Image;
import ohos.agp.utils.Rect;
import ohos.agp.window.service.WindowManager;
import ohos.ai.cv.common.*;
import ohos.ai.cv.qrcode.IBarcodeDetector;
import ohos.ai.cv.text.ITextDetector;
import ohos.ai.cv.text.Text;
import ohos.ai.cv.text.TextConfiguration;
import ohos.ai.cv.text.TextDetectType;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;

public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        findComponentById(ResourceTable.Id_text_helloworld1)
                .setClickedListener(c -> {
                    present(new SecondAbilitySlice(), new Intent());
                });



    }

    private void detectText() {
        // 在收到onServiceConnect回调连接服务成功后，实例化ITextDetector接口，将此工程的context作为入参
        ITextDetector textDetector = VisionManager.getTextDetector(this);
        // 实例化VisionImage对象image，并传入待检测图片pixelMap。
        Resource resource = null;
        try {
            resource = getResourceManager().getResource(ResourceTable.Media_aaa);
            ImageSource imageSource = ImageSource.create(resource, null);
            PixelMap pixelMap = imageSource.createPixelmap(null);
            VisionImage image = VisionImage.fromPixelMap(pixelMap);

            int detect = textDetector.detect(image, null, callback);
            textDetector.release();
            pixelMap.release();
        } catch (IOException|NotExistException e) {
            e.printStackTrace();
        }finally {
            try {

                resource.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    VisionCallback<Text> callback= new VisionCallback<Text>() {
        @Override
        public void onResult(Text text) {
            // 对正确获得文字识别结果进行处理。
            System.out.println(text.getValue());
        }

        @Override
        public void onError(int i) {
            // 处理错误返回码。
        }

        @Override
        public void onProcessing(float v) {
            // 返回处理进度。
        }
    };



    // 定义ConnectionCallback回调，实现连接能力引擎成功与否后的操作。
    ConnectionCallback connectionCallback = new ConnectionCallback() {
        @Override
        public void onServiceConnect() {
            // Do something when service connects successfully
//            PixelMap pm = createQR("hello 你好啊");
//            ((Image)findComponentById(ResourceTable.Id_image_qr)).setPixelMap(pm);
            // 文字识别0
            // detectText();
        }

        @Override
        public void onServiceDisconnect() {
            // Do something when service connects unsuccessfully
        }
    };

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
